package com.stdroom.hellomyfriend.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import com.stdroom.hellomyfriend.MyApplication;
import com.stdroom.hellomyfriend.R;
import com.stdroom.hellomyfriend.bean.ShortMessage;
import com.stdroom.hellomyfriend.common.SaxParse;
import com.stdroom.hellomyfriend.common.SendMessage;
import com.stdroom.hellomyfriend.widget.DragSortListView;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class FragmentSendEvents extends Fragment{
	
	private ArrayList<ShortMessage> messages = null;
	private ShortMessageAdapter adapter = null;
	private DragSortListView dragSortLv;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d("hah","shit");
		View view = inflater.inflate(R.layout.message_wrap_list_layout, container, false);
		
		dragSortLv = (DragSortListView)view.findViewById(R.id.dragSortLv);
		
		String path = Environment.getExternalStorageDirectory().getAbsolutePath();
		File file = new File(path+"/message.xml");
		FileInputStream fis;
		try {
			fis = new FileInputStream(file);
			messages = SaxParse.SaxReadXml(fis);
			adapter = new ShortMessageAdapter(messages);
			Log.d("message",Integer.toString(messages.size()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		dragSortLv.setDropListener(dropListener);
		dragSortLv.setRemoveListener(removeListener);
		dragSortLv.setAdapter(adapter);
		
		return view;
	}
	private DragSortListView.DropListener dropListener = new DragSortListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			ShortMessage item = adapter.getItem(from);
			adapter.remove(item);
			adapter.insert(item, to);
		}
	};
	private DragSortListView.RemoveListener removeListener = new DragSortListView.RemoveListener() {
		@Override
		public void remove(int which) {
			messages.remove(which);
		}
	};
	
	private class ViewHolder{
		public TextView itemView;
	}
	private class ShortMessageAdapter extends ArrayAdapter<ShortMessage>{


		public ShortMessageAdapter(ArrayList<ShortMessage> message){
			super(MyApplication.getInstance().getApplicationContext()
					,R.layout.message_wrap_list_item_layout
					,R.id.messageContent_Tv, message);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v =  super.getView(position, convertView, parent);
			if(v!=null && v!=convertView){
				ViewHolder viewHolder = new ViewHolder();
				TextView tv = (TextView)v.findViewById(R.id.messageContent_Tv);
				viewHolder.itemView = tv;
				v.setTag(viewHolder);
			}
			
			ViewHolder holder = (ViewHolder)v.getTag();
			holder.itemView.setText(getItem(position).getData());
			return v;
			
		}
	}
}
